
//
//  LHChannelControl.m
//  channelDemo
//
//  Created by zhouLiHuan on 2017/2/7.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "LHChannelControl.h"
#import "LHChannelViewController.h"
#import "LHBaseNavigationController.h"

#define MenuItemsDic [NSString pathWithComponents:@[NSHomeDirectory(),@"/Documents/MenuItems"]]
#define InUseItemsPath [NSString pathWithComponents:@[NSHomeDirectory(),@"/Documents/MenuItems/InUsesItems.plist"]]
#define UnUseItemsPath [NSString pathWithComponents:@[NSHomeDirectory(),@"/Documents/MenuItems/UnUseItems.plist"]]

@implementation LHChannelControl

+ (LHChannelControl *)shareControl{
    
    static LHChannelControl *control = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        control = [LHChannelControl new];
    });
    
    return control;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        [self initSavepath];
        
    }
    return self;
}


- (void)initSavepath{
    //初始化本文件夹
    if (![[NSFileManager defaultManager] fileExistsAtPath:InUseItemsPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:MenuItemsDic withIntermediateDirectories:YES attributes:nil error:nil];
        NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:[NSMutableArray new]];
        NSData *data2 = [NSKeyedArchiver archivedDataWithRootObject:[NSMutableArray new]];
        [data1 writeToFile:InUseItemsPath atomically:YES];
        [data2 writeToFile:UnUseItemsPath atomically:YES];
    }
    
    NSLog(@"本地菜单地址: %@",InUseItemsPath);
    
}

#pragma mark- 重写setter方法
- (void)setInUseItems:(NSMutableArray *)inUseItems{
    [[NSKeyedArchiver archivedDataWithRootObject:inUseItems] writeToFile:InUseItemsPath atomically:YES];
}

- (void)setUnUseItems:(NSMutableArray *)unUseItems{
    [[NSKeyedArchiver archivedDataWithRootObject:unUseItems] writeToFile:UnUseItemsPath atomically:YES];
}

#pragma mark - 重写getter方法

- (NSMutableArray *)inUseItems{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:InUseItemsPath];
}

- (NSMutableArray *)unUseItems{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:UnUseItemsPath];
}


#pragma mark - 显示方法

- (void)showInViewController:(UIViewController *)vc completion:(ChannelBlock)channels{
    LHChannelViewController *channelVc = [[LHChannelViewController alloc] init];
    [channelVc addBackBlock:^{
        
        if (channels) {
            channels(self.inUseItems);
        }
    }];
    
    LHBaseNavigationController *nav = [[LHBaseNavigationController alloc] initWithRootViewController:channelVc];
    [vc presentViewController:nav animated:YES completion:nil];
    
}

@end

